package com.huas.xml;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class DomTest
{

	/**
	 * @param args
	 * @throws ParserConfigurationException
	 */
	public static void main(String[] args) throws Exception
	{
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder builder = builderFactory.newDocumentBuilder();
		// doc是整个文档的根节点
		Document doc = builder.parse(new File("build.xml"));
		// 获得根元素节点
		Element root = doc.getDocumentElement();
		System.out.println(root.getTagName());
		NodeList list = doc.getElementsByTagName("target");
		for (int i = 0; i < list.getLength(); i++)
		{
			Element e = (Element) list.item(i);
			// System.out.println(e.getFirstChild());
			NodeList list2 = e.getElementsByTagName("java");
			for (int j = 0; j < list2.getLength(); j++)
			{
				Element el = (Element) list2.item(j);
				System.out.println(el.getFirstChild().getNodeValue());
			}
		}
	}

}
